package com.shell.qq;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.shell.qq.sql.DBHelper;
import com.shell.qq.sql.DBUtil;

import java.io.IOException;

//注册页面
public class RegActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText username;
    private EditText password;
    private EditText phone;
    private EditText Email;
    private Button btnSubmit;
    private Button btnReturn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_reg);
        init();
    }

    public void init() {
        Intent intent = getIntent();
        username = findViewById(R.id.username);
        username.setText(intent.getStringExtra("username"));//如果注册之前，填写了用户名，则自动填充过来
        password = findViewById(R.id.password);
        password.setText(intent.getStringExtra("password"));//如果注册之前，填写了密码，则自动填充过来
        phone = findViewById(R.id.phone);
        Email = findViewById(R.id.Email);
        btnSubmit = findViewById(R.id.btn_submit);
        btnReturn = findViewById(R.id.btn_return);
        btnSubmit.setOnClickListener(this);//绑定监听器到当前类
        btnReturn.setOnClickListener(this);//绑定监听器到当前类
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_return:
                onClickReturn();
                break;
            case R.id.btn_submit:
                onClickSubmit();
                break;
        }
    }

    //点击返回按钮
    public void onClickReturn() {
        Intent intent = new Intent();
        intent.putExtra("username", username.getText().toString());
        intent.putExtra("password", password.getText().toString());
        setResult(1, intent);
        finish();//配合上一页面的startActivityForResult(intent, 0);可以实现返回
    }

    //点击提交
    public void onClickSubmit() {
        if (username.getText().toString() .equals("")) {
            Toast.makeText(RegActivity.this,"用户名不能为空！",Toast.LENGTH_LONG).show();
            return;
        }
        if (password.getText().toString() .equals("")) {
            Toast.makeText(RegActivity.this,"密码不能为空！",Toast.LENGTH_LONG).show();
            return;
        }
        if (phone.getText().toString() .equals("")) {
            Toast.makeText(RegActivity.this,"手机号不能为空！",Toast.LENGTH_LONG).show();
            return;
        }
        if (Email.getText().toString() .equals("")) {
            Toast.makeText(RegActivity.this,"邮箱不能为空！",Toast.LENGTH_LONG).show();
            return;
        }
        Bundle bundle=new Bundle();//相当于一个Map集合
        bundle.putString("username",username.getText().toString());
        bundle.putString("password",password.getText().toString());
        bundle.putString("phone",phone.getText().toString());
        bundle.putString("Email",Email.getText().toString());

        saveToSQLite();//保存信息到数据库

//        Intent intent = new Intent(this, RegSuccessActivity.class);//不存储信息，仅仅传递到下一个activity
        Intent intent = new Intent(this, RegsuccActivity.class);//以XML文件形式存放注册信息，并跳转到另一个activity
        intent.putExtras(bundle); //与putExtra()不同，多了一个s
        User user = new User(username.getText().toString(), password.getText().toString(), phone.getText().toString(), Email.getText().toString());
        UserInfo userInfo = new UserInfo(user);
        userInfo.saveUserXMLDate(this);//将用户信息保存到XML文件
        try {
            userInfo.saveUserDateWithFile(this);//将用户信息保存到文件中（内部存储）
        } catch (IOException e) {
            e.printStackTrace();
        }
        startActivityForResult(intent, 2);
    }
    //存储到数据库
    public void saveToSQLite(){
        DBHelper dbHelper=new DBHelper(this);
        String username=this.username.getText().toString();
        String password=this.password.getText().toString();
        String phone=this.phone.getText().toString();
        String Email=this.Email.getText().toString();
        SQLiteDatabase db;
        db = dbHelper.getWritableDatabase();//获取数据库对象，相当于连接数据库
        ContentValues values = new ContentValues();
        values.put(DBUtil.USER_ID,username);
        values.put(DBUtil.USER_PWD,password);
        values.put(DBUtil.USER_PHONE,phone);
        values.put(DBUtil.USER_EMAIL,Email);
        //表名，忽略的字段，值
        db.insert(DBUtil.TABLE_NAME, null, values);
        db.close();//关闭连接
    }
}